package com.newcoin.chat.dao;

import com.newcoin.chat.beans.entity.FriendMsgEntity;
import com.newcoin.chat.beans.entity.GroupMsgEntity;
import com.newcoin.chat.beans.vo.FriendOfflineMsgVo;
import com.newcoin.chat.beans.vo.GroupOfflineMsgVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface ChatDao {
    /**
     * 好友消息
     *
     * @param userId
     * @return
     */
    List<FriendOfflineMsgVo> getFriendOfflineMsg(@Param("userId") long userId);

    /**
     * 群组消息
     *
     * @param userId
     * @return
     */
    List<GroupOfflineMsgVo> getGroupOfflineMsg(@Param("userId") long userId);

    /**
     * 保存好友消息/系统好友消息
     *
     * @param friendMsgEntity
     * @return
     */
    int addFriendMsg(FriendMsgEntity friendMsgEntity);


    @Update("UPDATE friend_relationship SET unread_message_num=unread_message_num+#{num},last_message_id=#{msgId},last_message_time=UNIX_TIMESTAMP(NOW()) WHERE user_id=#{userId} AND friend_id=#{friendId}")
    int updateFriendLastMsgNumAndLastMsgId(@Param("userId") long userId, @Param("friendId") long friendId, @Param("msgId") long msgId, @Param("num") int num);

    /**
     * 获取群成员
     *
     * @param groupId
     * @return
     */
    @Select("SELECT user_id FROM group_member WHERE group_id=#{groupId}")
    List<Long> getGroupMember(@Param("groupId") long groupId);

    /**
     * 更新本人在该群得未读条数和最后一条群消息得id
     *
     * @param groupId
     * @param userId
     * @return
     */
    @Update("UPDATE group_member SET unread_message_num=unread_message_num+1,last_message_id=#{msgId},last_message_time=UNIX_TIMESTAMP(NOW()) WHERE group_id=#{groupId} and user_Id=#{userId} ")
    int updateGroupLastMsgNumAndLastMsgId(@Param("groupId") long groupId, @Param("userId") long userId, @Param("msgId") long msgId);

    /**
     * 保存群消息/群系统消息
     *
     * @param groupMsgEntity
     * @return
     */
    int addGroupMsg(GroupMsgEntity groupMsgEntity);


}
